package com.ruoyi.system.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gg.reader.api.dal.GClient;
import com.gg.reader.api.dal.HandlerTagEpcLog;
import com.gg.reader.api.dal.HandlerTagEpcOver;
import com.gg.reader.api.protocol.gx.*;
import com.ruoyi.system.domain.TZhgdDeviceToolsInfo;

import java.util.ArrayList;
import java.util.List;

public class RFIDTcpClient {

   static String pathUrl="192.168.0.30:8160";
    static String HttpUrl="http://10.0.19.29:9089/main/wndlzhgd/hhwy.fweb.device.tools/storage/toRfid";
    private static GClient client = new GClient();

    public  String openService(String path) {
        GClient client = new GClient();
        System.out.println("RFIDTcpClient.openService--url:"+path);
        if (client.openTcp(path, 2000)) {
            subscribeHandler(client);
            MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc();
            msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1);
            msgBaseInventoryEpc.setInventoryMode(EnumG.InventoryMode_Inventory);
            client.sendSynMsg(msgBaseInventoryEpc);
            if (0x00 == msgBaseInventoryEpc.getRtCode()) {
                System.out.println("仓储设备MsgBaseInventoryEpc[OK].");
                return "仓储设备已经连接成功";

            } else {
                System.out.println("存在连接，创建新连接！");
                closeClent(path,true);
            }
        }
        return "仓储设备已经连接成功";
    }

    public static String closeClent(String path,boolean flag){
        MsgBaseStop stopMsg = new MsgBaseStop();
        client.sendSynMsg(stopMsg);
        if (0x00 == stopMsg.getRtCode()) {
            System.out.println("关闭仓储设备连接");
        } else {
            System.out.println("关闭仓储设备连接 Fail，错误代码"+stopMsg.getRtCode());
            return "关闭仓储设备连接 Fail";
        }
        client.close();
//        if(flag){
//            openService(path);
//        }
        return "关闭仓储设备连接";
    }


    //订阅6c标签信息上报
    static void subscribeHandler(GClient client) {
        client.onTagEpcLog = new HandlerTagEpcLog() {
            @Override
            public void log(String s, LogBaseEpcInfo logBaseEpcInfo) {
                if (null != logBaseEpcInfo && logBaseEpcInfo.getResult() == 0) {
                    System.out.println("设备EPc:"+logBaseEpcInfo.getbEpc().toString());
                    System.out.println("获取订阅数据"+logBaseEpcInfo);
                    logBaseEpcInfo.getEpc().toString();
                    System.out.println("--------EPC:"+logBaseEpcInfo.getEpc() );


                }
            }
        };

        client.onTagEpcOver = new HandlerTagEpcOver() {
            @Override
            public void log(String s, LogBaseEpcOver logBaseEpcOver) {
                System.out.println("获取订阅任务关闭");
            }
        };
    }


    public static void main(String[] args) {
        if (client.openTcp(pathUrl, 2000)) {
            subscribeHandler(client);
            MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc();
            msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1);
            msgBaseInventoryEpc.setInventoryMode(EnumG.InventoryMode_Inventory);
            client.sendSynMsg(msgBaseInventoryEpc);
            if (0x00 == msgBaseInventoryEpc.getRtCode()) {
                System.out.println("仓储设备MsgBaseInventoryEpc[OK].");
            } else {

                System.out.println("存在连接:"+msgBaseInventoryEpc);
                System.out.println("--------EPC:"+msgBaseInventoryEpc.getRtCode());

                //closeClent(pathUrl,true);
            }
        }
        try {
            Thread.sleep(12000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        closeClent(pathUrl,false);
//        ArrayList<TZhgdDeviceToolsInfo> studentLsit = new ArrayList<>();
//        TZhgdDeviceToolsInfo student1 = new TZhgdDeviceToolsInfo();
//
//        student1.setId("123");
//        student1.setEpc("3333333");
//        student1.setProjectId("12312");
//        student1.setCnt("胜多负少");
//
//        studentLsit.add(student1);
//
//        TZhgdDeviceToolsInfo student2 = new TZhgdDeviceToolsInfo();
//        student2.setId("2222");
//        student2.setEpc("4444444");
//        student2.setProjectId("34467657");
//        student2.setCnt("哦类最足可以");
//
//        studentLsit.add(student2);
//
//
//        JSONObject obj = new JSONObject();
//        obj.put("toolsInfo",studentLsit);
//
//        //list转json字符串
//
//        String string = obj.toString();
//
//        System.out.println(string);

        //json字符串转listJson格式



//        JSONObject jsonData = JSONObject.parseObject(string);
//        jsonData.keySet().forEach(key->{
//            jsonData.get(key);
//            if("toolsInfo".equals(key)){
//                List<TZhgdDeviceToolsInfo> personVos = JSONObject.parseArray( jsonData.get(key).toString(), TZhgdDeviceToolsInfo.class);
//            }
//
//            if("toolsStorage".equals(key)){
//                List<TZhgdDeviceToolsInfo> personVos = JSONObject.parseArray( jsonData.get(key).toString(), TZhgdDeviceToolsInfo.class);
//            }
//            if("storageHistory".equals(key)){
//                List<TZhgdDeviceToolsInfo> personVos = JSONObject.parseArray( jsonData.get(key).toString(), TZhgdDeviceToolsInfo.class);
//            }
//        });
//
//        System.out.println(jsonData);
//        List<TZhgdDeviceToolsInfo> personVos = JSONObject.parseArray(jsonData.get("toolsInfo").toString(), TZhgdDeviceToolsInfo.class);
//        System.out.println(personVos);



    }
}
